package com.hghivln.erpwarehouse.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hghivln.erpwarehouse.business.entity.*;
import com.hghivln.erpwarehouse.business.entity.Outport;
import com.hghivln.erpwarehouse.business.mapper.OutportMapper;
import com.hghivln.erpwarehouse.business.service.GoodsService;
import com.hghivln.erpwarehouse.business.service.InportService;
import com.hghivln.erpwarehouse.business.service.OutportService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hghivln.erpwarehouse.business.service.ProviderService;
import com.hghivln.erpwarehouse.business.vo.OutportVo;
import com.hghivln.erpwarehouse.system.common.lang.ActiverUser;
import com.hghivln.erpwarehouse.system.common.lang.DataGridlView;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hghivln
 * @since 2021-07-10
 */
@Service
public class OutportServiceImpl extends ServiceImpl<OutportMapper, Outport> implements OutportService {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ProviderService providerService;

    @Autowired
    private InportService inportService;

    @Override
    public DataGridlView queryAllOutport(OutportVo outportVo) {
        IPage<Outport> page=new Page<>(outportVo.getPage(),outportVo.getLimit());
        QueryWrapper<Outport> qw=new QueryWrapper<>();
        qw.eq(outportVo.getGoodsid()!=null,"goodsid",outportVo.getGoodsid());
        qw.eq(outportVo.getProviderid()!=null,"providerid",outportVo.getProviderid());

        qw.ge(outportVo.getStartDate()!=null,"outporttime",outportVo.getStartDate());
        qw.le(outportVo.getEndDate()!=null,"outporttime",outportVo.getEndDate());

        this.getBaseMapper().selectPage(page,qw);
        List<Outport> records = page.getRecords();
        for (Outport record : records) {
            if(null!=record.getGoodsid()){
                Goods goods = this.goodsService.getById(record.getGoodsid());
                record.setGoodsname(goods.getGoodsname());
                record.setSize(goods.getSize());
            }
            if(null!= record.getProviderid()){
                Provider provider = this.providerService.getById(record.getProviderid());
                record.setProvidername(provider.getProvidername());
            }
        }
        return new DataGridlView(page.getTotal(),records);
    }

    @Override
    public Outport addOutport(Outport outport) {

        Inport inport = inportService.getById(outport.getInportid());
        Goods goods = goodsService.getById(inport.getGoodsid());

        goods.setNumber(goods.getNumber()-outport.getNumber());

        goodsService.updateGoods(goods);

        outport.setPaytype(inport.getPaytype());
        outport.setOutportprice(inport.getInportprice());
        outport.setProviderid(inport.getProviderid());
        outport.setOutporttime(new Date());
        ActiverUser activerUser = (ActiverUser) SecurityUtils.getSubject().getPrincipal();
        outport.setOperateperson(activerUser.getUser().getName());

        getBaseMapper().insert(outport);
        return outport;
    }

}
